let jjModel = require('./db/model')

async function readData(page) {
  return new Promise(async (resolve, reject)=>{
    // await utils.wait(1000)
    // page.once('domcontentloaded', async()=>{
    //   let obj = await page.$eval('.fundDetail-tit', (node)=>{
    //     let text = node.innerText
    //     let arr = text.match(/(.+)\((\d+)\n?/) || []
    //     return {
    //       name: arr[1] || '错误',
    //       code: arr[2] || '错误'
    //     }
    //   })
    //   let size  = await page.$eval('.infoOfFund table', node =>{
    //     let trs = node.querySelectorAll('tr')
    //     let tds = trs[0].querySelectorAll('td')
    //     let num = tds[1].innerText.match(/：(.+)亿/)[1] || 0
    //     return parseFloat(num) 
    //   })
    //   let item = await page.$eval('.poptableWrap', (node) => {
    //     let trlist = Array.from(node.querySelectorAll('tbody tr')).slice(1)
    //     let box = {}
    //     trlist.forEach((tr)=>{
    //       let td = tr.querySelectorAll('td')
    //       if(td.length<2) {
    //         return
    //       }
    //       let name = td[0].innerText
    //       let num = parseFloat(td[1].innerText.slice(0, -1))
    //       if(box[name]) {
    //         box[name] += num
    //       }else{
    //         box[name] = num
    //       }
    //     })
    //     return box
    //   })
    //   await page.close()
    //   resolve({
    //     ...obj,
    //     size,
    //     list: item
    //   })
    // })

    let obj = await page.$eval('.fundDetail-tit', (node)=>{
      let text = node.innerText
      let arr = text.match(/(.+)\((\d+)\n?/) || []
      return {
        name: arr[1] || '错误',
        code: arr[2] || '错误'
      }
    })
    let size  = await page.$eval('.infoOfFund table', node =>{
      let trs = node.querySelectorAll('tr')
      let tds = trs[0].querySelectorAll('td')
      let num = tds[1].innerText.match(/：(.+)亿/)[1] || 0
      return parseFloat(num) 
    })
    let item = await page.$eval('.poptableWrap', (node) => {
      let trlist = Array.from(node.querySelectorAll('tbody tr')).slice(1)
      let box = {}
      trlist.forEach((tr)=>{
        let td = tr.querySelectorAll('td')
        if(td.length<2) {
          return
        }
        let name = td[0].innerText
        let num = parseFloat(td[1].innerText.slice(0, -1))
        if(box[name]) {
          box[name] += num
        }else{
          box[name] = num
        }
      })
      return box
    })
    await page.close()
    resolve({
      ...obj,
      size,
      list: item
    })
    
  })      
}

async function openNewTab(browser, ele, index) {
  console.log('来了嘛')
  return new Promise(async (resolve, reject)=>{
    // browser.once('targetcreated', async (target)=>{
    //   try {
    //     // console.log('打开新页面')
    //     if(target.type()==='other') return 
    //     let page = await target.page()
    //     let data = await readData(page)
    //     var model = new jjModel(data)
    //     model.save(function (err) {
    //       if (err) return handleError(err);
    //     })
    //     console.log('保存数据')
    //     resolve()
    //   } catch (error) {
    //     console.log('出错？', error)
    //     reject(error)
    //   }
    // })
    // let eleTds = await ele[index].$$('td')
    // let linkA = await eleTds[3].$('a')
    // await linkA.click()
    let url = await ele[index].$eval('td a', node=>{
      return node.getAttribute('href')
    })
    // console.log('url',url)
    const page = await browser.newPage();
    await page.goto(url);
    let data = await readData(page)
    var model = new jjModel(data)
    model.save(function (err) {
      if (err) return console.log(err);
    })
    console.log('保存数据')
    resolve()
  })

  
}

module.exports = {readData, openNewTab}